package jmine.tec.database.pack.services;

import static jmine.tec.database.pack.PackingMessages.IO_EXCEPTION;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.populator.AbstractPopulatorWriterSource;
import jmine.tec.services.api.export.ExportActor;
import jmine.tec.services.io.ServiceBean;
import jmine.tec.services.io.ServicesPageBean;
import jmine.tec.services.io.ServicesWriter;

/**
 * Implementação de PopulatorWriter que escreve em xml de serviços.
 * 
 * @author lundberg
 */
public class ServicePopulatorWriter extends AbstractPopulatorWriterSource<Object> {

    private ExportActor exportActor;

    private ServicesWriter serviceWriter;

    /**
     * {@inheritDoc}
     */
    public void write(Object source, OutputStream output) throws PackException {
        List<ServiceBean> services = this.exportActor.export(Collections.singletonList(source));
        ServicesPageBean page = new ServicesPageBean();
        page.addServices(services);
        try {
            this.serviceWriter.writeServices(Collections.singletonList(page), output);
        } catch (IOException e) {
            throw new PackException(IO_EXCEPTION, e);
        }
    }

    /**
     * @param exportActor the exportActor to set
     */
    public void setExportActor(ExportActor exportActor) {
        this.exportActor = exportActor;
    }

    /**
     * @param serviceWriter the serviceWriter to set
     */
    public void setServiceWriter(ServicesWriter serviceWriter) {
        this.serviceWriter = serviceWriter;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "ServicePopulatorWriter";
    }
}
